package com.easy.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.easy.MslApp;
import com.easy.R;
import com.easy.base.activity.BaseActivity_noMVP;
import com.easy.base.util.Util;
import com.easy.dialog.ChooseDialog;
import com.easy.ui.query.QueryActivity;
import com.shelwee.update.UpdateHelper;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity_noMVP{
    private String tag = "MainActivity";
    @Bind(R.id.tvright)
    TextView tvright;
    @Bind(R.id.imageView1)
    ImageView imageView1;
    @Bind(R.id.text1)
    TextView text1;
    @Bind(R.id.ll1)
    LinearLayout ll1;
    @Bind(R.id.imageView2)
    ImageView imageView2;
    @Bind(R.id.text2)
    TextView text2;
    @Bind(R.id.ll2)
    LinearLayout ll2;
    @Bind(R.id.imageView3)
    ImageView imageView3;
    @Bind(R.id.text3)
    TextView text3;
    @Bind(R.id.ll3)
    LinearLayout ll3;
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv)
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        tvTitle.setText("主页");
        tvright.setVisibility(View.VISIBLE);
        tvright.setText("退出账号");
        tvright.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChooseDialog nDialogr = new ChooseDialog(MainActivity.this, "退出", R.style.Dialog1, new ChooseDialog.PriorityListener() {
                    @Override
                    public void setActivityText() {
                        MslApp.sEditor.putInt(Util.userID, -1);
                        MslApp.sEditor.putString(Util.userk, "");
                        MslApp.sEditor.putString(Util.pwk, "");
                        MslApp.sEditor.commit();
                        finish();
                    }
                });
                WindowManager.LayoutParams lpr = nDialogr.getWindow().getAttributes();
                lpr.gravity = Gravity.CENTER;
                nDialogr.setCanceledOnTouchOutside(false);
                nDialogr.show();
            }
        });
        Glide.with(this).load(MslApp.sSharedPreferences.getString(Util.url_image,"")).into(iv);

//        UpdateHelper updateHelper = new UpdateHelper.Builder(this)
//                .checkUrl(MslApp.sSharedPreferences.getString(Util.addk, "") +Util.url_appupdate)
//                .isAutoInstall(true) //设置为false需在下载完手动点击安装;默认值为true，下载后自动安装。
//                .build();
//        updateHelper.check();
    }

    @OnClick({R.id.iv_title_left,R.id.ll1, R.id.ll2, R.id.ll3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                ChooseDialog nDialog = new ChooseDialog(this, "退出", R.style.Dialog1, new ChooseDialog.PriorityListener() {
                    @Override
                    public void setActivityText() {
                        finish();
                    }
                });
                WindowManager.LayoutParams lp = nDialog.getWindow().getAttributes();
                lp.gravity = Gravity.CENTER;
                nDialog.setCanceledOnTouchOutside(false);
                nDialog.show();
                break;
            case R.id.ll1:
                Intent intent = new Intent();
                intent.setClass(this, QueryActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            ChooseDialog nDialog = new ChooseDialog(this, "退出", R.style.Dialog1, new ChooseDialog.PriorityListener() {
                @Override
                public void setActivityText() {
                    finish();
                }
            });
            WindowManager.LayoutParams lp = nDialog.getWindow().getAttributes();
            lp.gravity = Gravity.CENTER;
            nDialog.setCanceledOnTouchOutside(false);
            nDialog.show();
        }
        return super.onKeyDown(keyCode, event);
    }
}
